/* Copyright (c) 2004-2024 Peigen.info. All rights reserved. */

package info.peigen.hotpot.component.mail.manage.service;

import info.peigen.hotpot.common.core.result.PageResult;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceContext;
import info.peigen.hotpot.common.service.base.processor.ProcessorServiceInvoke;
import info.peigen.hotpot.component.mail.manage.service.order.MailTemplateCreateOrder;
import info.peigen.hotpot.component.mail.manage.service.order.MailTemplateDeleteOrder;
import info.peigen.hotpot.component.mail.manage.service.order.MailTemplateEditOrder;
import info.peigen.hotpot.component.mail.manage.service.result.MailTemplateCreateResult;
import info.peigen.hotpot.component.mail.manage.service.result.MailTemplateDeleteResult;
import info.peigen.hotpot.component.mail.manage.service.result.MailTemplateEditResult;
import info.peigen.hotpot.component.mail.manage.controller.form.MailTemplateManageCreateForm;
import info.peigen.hotpot.component.mail.manage.controller.form.MailTemplateManageDeleteForm;
import info.peigen.hotpot.component.mail.manage.controller.form.MailTemplateManageEditForm;
import info.peigen.hotpot.component.mail.manage.controller.form.MailTemplateManageSearchForm;
import info.peigen.hotpot.component.mail.manage.service.result.info.MailTemplateManageInfo;
import info.peigen.hotpot.component.mail.repository.service.MailTemplateRepository;
import info.peigen.hotpot.component.mail.service.result.MailResult;
import org.noear.solon.annotation.Component;
import org.noear.solon.annotation.Inject;

/**
 * <b>(MailTemplateManageService)</b>
 *
 * @author Peigen
 * @version 1.0.0
 * @since 2023/1/6
 */
@SuppressWarnings({"AlibabaServiceOrDaoClassShouldEndWithImpl", "rawtypes"})
@Component
public class MailTemplateManageService extends AbstractMailTempManageService {
    @Inject
    private MailTemplateRepository mailTemplateRepository;

    public PageResult<MailTemplateManageInfo> page(MailTemplateManageSearchForm form) {
        // return mailTemplateRepository.page(form, MailTemplateManageConvertor::convertToInfo);
        return null;
    }

    public MailTemplateCreateResult create(MailTemplateManageCreateForm form) {
        MailTemplateCreateOrder order = MailTemplateManageConvertor.convertOrder(form);
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new MailResult())
                .build();

        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(generateMailDomainProcessor, sendMimeMailProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                ProcessorServiceInvoke.super.addPostProcessor(serviceContext);
                serviceContext.addProcessors(sendMailResultProcessor);
            }
        });

        return (MailTemplateCreateResult) serviceContext.getResult();
    }

    public MailTemplateEditResult edit(MailTemplateManageEditForm form) {
        MailTemplateEditOrder order = MailTemplateManageConvertor.convertOrder(form);
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new MailResult())
                .build();

        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(generateMailDomainProcessor, sendMimeMailProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                ProcessorServiceInvoke.super.addPostProcessor(serviceContext);
                serviceContext.addProcessors(sendMailResultProcessor);
            }
        });

        return (MailTemplateEditResult) serviceContext.getResult();
    }

    public MailTemplateDeleteResult delete(MailTemplateManageDeleteForm form) {
        MailTemplateDeleteOrder order = MailTemplateManageConvertor.convertOrder(form);
        ProcessorServiceContext serviceContext = ProcessorServiceContext.builder().order(order).result(new MailResult())
                .build();

        execute(serviceContext, new ProcessorServiceInvoke() {
            @Override
            public void addProcessor(ProcessorServiceContext serviceContext) {
                serviceContext.addProcessors(generateMailDomainProcessor, sendMimeMailProcessor);
            }

            @Override
            public void addPostProcessor(ProcessorServiceContext serviceContext) {
                ProcessorServiceInvoke.super.addPostProcessor(serviceContext);
                serviceContext.addProcessors(sendMailResultProcessor);
            }
        });

        return (MailTemplateDeleteResult) serviceContext.getResult();
    }
}